import 'dart:io';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

/// 全局日历视图
/// 显示日历视图
/// 通过按钮切换视图
/// 根据平台自动适配

class CalendarView extends StatefulWidget {
  int? day;

  Function(DateTime, String, DateTime, String) onInitDateChanged;
  Function(DateTime, String) onDateStartChanged;
  Function(DateTime, String) onDateEndChanged;

  CalendarView({super.key, this.day, required this.onInitDateChanged, required this.onDateStartChanged, required this.onDateEndChanged});

  @override
  State<CalendarView> createState() => _CalendarViewState();
}

class _CalendarViewState extends State<CalendarView> {
  late DateTime calFirstDate;
  late DateTime calLastDate;
  late DateTime startDate;
  late DateTime endDate;

  bool isHMS = false;

  @override
  void initState() {
    DateTime now = DateTime.now();
    calFirstDate = DateTime(now.year, 1, 1);
    if (widget.day != null) {
      calLastDate = DateTime(now.year + 1, now.month, widget.day!);
      startDate = DateTime(now.year, now.month, 1, 0, 0, 0);
    } else {
      calLastDate = DateTime(now.year + 1, now.month, now.day);

      DateTime today = DateTime.now();
      int currentWeekday = DateTime.now().weekday;
      int daysToSubtract = (currentWeekday + 3) % 7 + 7 - 3;
      DateTime lastWednesday = today.subtract(Duration(days: daysToSubtract));
      startDate = lastWednesday;
    }
    endDate = DateTime(now.year, now.month, now.day, 23, 59, 59);
    endDate = endDate.subtract(const Duration(days: 1));

    widget.onInitDateChanged(startDate, "${startDate.year}-${startDate.month.toString().padLeft(2, '0')}-${startDate.day.toString().padLeft(2, '0')} 00:00:00",
        endDate, "${endDate.year}-${endDate.month.toString().padLeft(2, '0')}-${endDate.day.toString().padLeft(2, '0')} 23:59:59");
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        _buildCalendar([
          SizedBox(
            width: 245,
            height: 200,
            child: CalendarDatePicker(
              initialDate: startDate,
              firstDate: calFirstDate,
              lastDate: calLastDate,
              onDateChanged: (DateTime value) {
                String dateStartStr = "";
                if(isHMS){
                  dateStartStr = "${value.year}-${value.month.toString().padLeft(2, '0')}-${value.day.toString().padLeft(2, '0')} 23:59:59";
                }else{
                  dateStartStr = "${value.year}-${value.month.toString().padLeft(2, '0')}-${value.day.toString().padLeft(2, '0')}";
                }
                widget.onDateStartChanged(value, dateStartStr);
              },
            ),
          ),
          SizedBox(
            width: 245,
            height: 200,
            child: CalendarDatePicker(
              initialDate: endDate,
              firstDate: calFirstDate,
              lastDate: calLastDate,
              onDateChanged: (DateTime value) {
                String dateStartStr = "";
                if(isHMS){
                  dateStartStr = "${value.year}-${value.month.toString().padLeft(2, '0')}-${value.day.toString().padLeft(2, '0')} 23:59:59";
                }else{
                  dateStartStr = "${value.year}-${value.month.toString().padLeft(2, '0')}-${value.day.toString().padLeft(2, '0')}";
                }

                widget.onDateEndChanged(value, dateStartStr);
              },
            ),
          ),
          Text("是否显示时分秒："),
          Checkbox(value: isHMS, onChanged: (value) => setState(() => isHMS = value!)),
        ]),
      ],
    );
  }

  Widget _buildCalendar(List<Widget> children) {
    if (kIsWeb) {
      return Row(crossAxisAlignment: CrossAxisAlignment.start, children: children);
    } else {
      if (Platform.isIOS || Platform.isAndroid) {
        return Column(children: children);
      } else {
        return Row(crossAxisAlignment: CrossAxisAlignment.start, children: children);
      }
    }
  }
}
